package main

import (
	"flag"
	"fmt"
	"net/http"
	_ "net/http/pprof"
	"os"
	"os/signal"
	"runtime"
	"syscall"

	"gitee.com/JMArch/rpcmon-gateway/gateway"
	"go.uber.org/zap"
)

var banner = `
---------------------------------------------------------
                    gateway
Version:	%v
BuildTime:	%v
GitHash:	%v
GOVersion:	%v
---------------------------------------------------------
`

//VERSION
var (
	VERSION    = "0.0.0"
	BUILD_DATE = "0000-00-00 00:00:00"
	GitHash    = "000000"
)

func PrintVersion() {
	fmt.Printf(banner, VERSION, BUILD_DATE, GitHash, runtime.Version())
}

func main() {
	help := flag.Bool("h", false, "usage info")
	version := flag.Bool("v", false, "version info")
	confPath := flag.String("c", "main.toml", "config path")

	flag.Parse()

	if *help {
		flag.Usage()
		return
	} else if *version {
		PrintVersion()
		return
	}

	go func() {
		err := http.ListenAndServe("0.0.0.0:9999", nil)
		if err != nil {
			panic("0.0.0.0:9999 inused")
		}
	}()

	srv, err := gateway.New(*confPath)
	if err != nil {
		panic(err)
		os.Exit(1)
	}

	srv.Run()

	ch := make(chan os.Signal, 1)
	signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
	sig := <-ch
	srv.Log.Info("Received signal", zap.String("signal", sig.String()))

	srv.Close()

	return
}
